{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Distance Between Bus Stops"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: distanceBetweenBusStops"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #公交站间的距离"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>环形公交路线上有&nbsp;<code>n</code>&nbsp;个站，按次序从&nbsp;<code>0</code>&nbsp;到&nbsp;<code>n - 1</code>&nbsp;进行编号。我们已知每一对相邻公交站之间的距离，<code>distance[i]</code>&nbsp;表示编号为&nbsp;<code>i</code>&nbsp;的车站和编号为&nbsp;<code>(i + 1) % n</code>&nbsp;的车站之间的距离。</p>\n",
    "\n",
    "<p>环线上的公交车都可以按顺时针和逆时针的方向行驶。</p>\n",
    "\n",
    "<p>返回乘客从出发点&nbsp;<code>start</code>&nbsp;到目的地&nbsp;<code>destination</code>&nbsp;之间的最短距离。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/09/08/untitled-diagram-1.jpg\" style=\"height: 240px; width: 388px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>distance = [1,2,3,4], start = 0, destination = 1\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>公交站 0 和 1 之间的距离是 1 或 9，最小值是 1。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/09/08/untitled-diagram-1-1.jpg\" style=\"height: 240px; width: 388px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>distance = [1,2,3,4], start = 0, destination = 2\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>公交站 0 和 2 之间的距离是 3 或 7，最小值是 3。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/09/08/untitled-diagram-1-2.jpg\" style=\"height: 240px; width: 388px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>distance = [1,2,3,4], start = 0, destination = 3\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>公交站 0 和 3 之间的距离是 6 或 4，最小值是 4。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n&nbsp;&lt;= 10^4</code></li>\n",
    "\t<li><code>distance.length == n</code></li>\n",
    "\t<li><code>0 &lt;= start, destination &lt; n</code></li>\n",
    "\t<li><code>0 &lt;= distance[i] &lt;= 10^4</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [distance-between-bus-stops](https://leetcode.cn/problems/distance-between-bus-stops/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [distance-between-bus-stops](https://leetcode.cn/problems/distance-between-bus-stops/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4]\\n0\\n1', '[1,2,3,4]\\n0\\n2', '[1,2,3,4]\\n0\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        dis1 = sum(distance[start:destination])\n",
    "        dis2 = sum(distance) - dis1\n",
    "        return min(dis1, dis2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start == destination:\n",
    "            return 0\n",
    "        st, de = min(start,destination), max(start,destination)\n",
    "        sum1 = sum(distance[st:de])\n",
    "        return min(sum1,sum(distance)-sum1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        sum_i = 0\n",
    "        if start > destination:\n",
    "            start,destination = destination,start\n",
    "        for i in range(start,destination):\n",
    "            sum_i = sum_i + distance[i]\n",
    "        sum_ls = sum(distance)\n",
    "        return min(sum_i,sum_ls - sum_i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        t = sum(distance)\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        ans = 0\n",
    "        for i in range(start, destination):\n",
    "            ans += distance[i]\n",
    "        return min(ans, t-ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        dis1 = sum(distance[start:destination])\n",
    "        dis2 = sum(distance[:start]) + sum(distance[destination:])\n",
    "        return min(dis1, dis2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\r\n",
    "        return min(sum(distance[min(start,destination):max(start,destination)]), sum(distance)-sum(distance[min(start,destination):max(start,destination)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        sum_i = 0\n",
    "        if start > destination:\n",
    "            start,destination = destination,start\n",
    "        for i in range(start,destination):\n",
    "            sum_i = sum_i + distance[i]\n",
    "        sum_ls = sum(distance)\n",
    "        return min(sum_i,sum_ls-sum_i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start>destination:\n",
    "            start,destination = destination,start\n",
    "        return min(sum(distance[start:destination]),sum(distance[:start])+sum(distance[destination:]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        x = min(start, destination)\n",
    "        y = max(start, destination)\n",
    "        p1 = sum(distance[x:y])\n",
    "        p2 = sum(distance) - p1\n",
    "        return min(p1, p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start == destination:\n",
    "            return 0\n",
    "        if start < destination:\n",
    "            x = sum(distance[start:destination])\n",
    "            y = sum(distance)-x\n",
    "            return min(x,y)\n",
    "        else:\n",
    "            x = sum(distance[destination:start])\n",
    "            y = sum(distance)-x\n",
    "            return min(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start,destination = destination,start\n",
    "        return min(sum(distance[start:destination]),sum(distance) - sum(distance[start:destination]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        dis1=0\n",
    "        for i in range(min(start,destination),max(start,destination)):\n",
    "            dis1=dis1+distance[i]\n",
    "        dis2=sum(distance)-dis1\n",
    "        return min(dis1,dis2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        temp1 = 0\n",
    "        total = sum(distance)\n",
    "        if start>destination:\n",
    "            start,destination = destination, start\n",
    "        for i in range(start,destination):\n",
    "            temp1 += distance[i]\n",
    "        return min(temp1,total-temp1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        a, b = min(start, destination), max(start, destination)\n",
    "        distance1 = sum(distance[a: b])\n",
    "        distance2 = sum(distance) - distance1\n",
    "        return min(distance1, distance2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        n = len(distance)\n",
    "        distance += distance\n",
    "        # print(distance[start:destination])\n",
    "        # print(distance[destination:start+n])\n",
    "        a = min(start,destination)\n",
    "        b = max(start,destination)\n",
    "        return min(sum(distance[a:b]),sum(distance[b:a+n]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        sum_i = 0\n",
    "        if start > destination:\n",
    "            start,destination = destination,start\n",
    "        for i in range(start,destination):\n",
    "            sum_i = sum_i + distance[i]\n",
    "        sum_ls = sum(distance)\n",
    "        return min(sum_i,sum_ls - sum_i)'''\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        n = len(distance)\n",
    "        if start > destination :\n",
    "            start,destination = destination, start\n",
    "        # x1 = 0\n",
    "        # for i in range(start,destination):\n",
    "        #     x1 += distance[i]\n",
    "        # x2 = 0\n",
    "        # for i in range(0,n):\n",
    "        #     x2 += distance[i]\n",
    "        # x2 -= x1\n",
    "        # return min(x1,x2)\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        return min(sum(distance[min(start,destination):max(start,destination)]), sum(distance) - sum(distance[min(start,destination):max(start,destination)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        dis1 = sum(distance[start:destination])\n",
    "        dis2 = sum(distance) - dis1\n",
    "        return min(dis1, dis2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        p1 = sum(distance[min(start, destination):max(start, destination)])\n",
    "        return min(sum(distance)-p1, p1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:       \n",
    "            start, destination = destination, start\n",
    "        d1 = 0\n",
    "        for i in range(start, destination):\n",
    "            d1 += distance[i]\n",
    "        d2 = 0\n",
    "        for i in range(destination, len(distance)):\n",
    "            d2 += distance[i]\n",
    "        for i in range(0, start):\n",
    "            d2 += distance[i]\n",
    "        return min(d1, d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            tmp = destination\n",
    "            destination = start\n",
    "            start = tmp\n",
    "        shunshizhen = sum(distance[start:destination])\n",
    "        nishizhen = sum(distance[:start]) + sum(distance[destination:])\n",
    "        ans = min(shunshizhen,nishizhen)\n",
    "        print(start,destination,shunshizhen,nishizhen)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start>destination:\n",
    "            destination,start = start,destination\n",
    "        return min(sum(distance[start:destination]),sum(distance[:start]+distance[destination:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        total = sum(distance)\n",
    "        if destination < start:\n",
    "            start, destination = destination, start\n",
    "        d = sum(distance[start:destination])\n",
    "        return min(total-d, d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        tmp=distance+distance\n",
    "        one,two=0,0\n",
    "        if start<destination:\n",
    "            for i in range(start,destination):\n",
    "                one=one+distance[i]\n",
    "            two=sum(distance)-one\n",
    "        else:\n",
    "            for i in range(destination,start):\n",
    "                one=one+distance[i]\n",
    "            two=sum(distance)-one\n",
    "        return min(one,two)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start,destination = destination,start\n",
    "        return min(sum(distance[start:destination]),sum(distance[:start]) + sum(distance[destination:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start>destination:\n",
    "            t=start;start=destination;destination=t\n",
    "        return min(sum(distance)-sum(distance[start:destination]),sum(distance[start:destination]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if(start > destination):\n",
    "            start, destination = destination, start\n",
    "        a = sum(distance[start:destination])\n",
    "        b = sum(distance[destination:]) + sum(distance[:start])\n",
    "\n",
    "        return(min(a,b))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        len_= len(distance)\n",
    "        distance.extend(distance)\n",
    "        if start<destination:\n",
    "            one = sum(distance[start:destination])\n",
    "            # print(distance)\n",
    "            two = sum(distance[destination:(len_+start)])\n",
    "            return min(one,two)\n",
    "        else:\n",
    "            one = sum(distance[destination:start])\n",
    "            two = sum(distance[start:(len_+destination)])\n",
    "            return min(one, two)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start == destination:\n",
    "            return 0\n",
    "        st, de = min(start,destination), max(start,destination)\n",
    "        sum1 = sum(distance[st:de])\n",
    "        return min(sum1,sum(distance)-sum1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if destination > start:\n",
    "            return min(sum(distance[start: destination]), sum(distance) - sum(distance[start: destination]))\n",
    "        else:\n",
    "            return min(sum(distance[destination: start]), sum(distance) - sum(distance[destination: start]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        total, s, start, destination = 0, 0, min(start, destination), max(start, destination)\n",
    "        for i, j in enumerate(distance):\n",
    "            total +=  j\n",
    "            if start <= i < destination:\n",
    "                s  += j\n",
    "        return min(s, total - s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        s=sum(distance)\n",
    "        if start>destination:\n",
    "            start,destination=destination,start\n",
    "        r=0\n",
    "        while start<destination:\n",
    "            r+=distance[start]\n",
    "            start+=1\n",
    "        return min(r,s-r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        dis=0\n",
    "        sum=0\n",
    "        for i in distance:\n",
    "            sum+=i \n",
    "        i=start\n",
    "        while i!=destination:\n",
    "            dis+=distance[i]\n",
    "            i+=1\n",
    "            if(i>=len(distance)):\n",
    "                i%=len(distance)\n",
    "        return dis if dis<sum-dis else sum-dis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        x = min(start, destination)\n",
    "        y = max(start, destination)\n",
    "        l1 = distance + distance\n",
    "        p1 = sum(distance[x:y])\n",
    "        p2 = sum(l1[y: x + len(distance)])\n",
    "        return min(p1, p2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        Sum=sum(distance)\n",
    "        if start>destination:\n",
    "            start,destination=destination,start\n",
    "        d=sum(distance[start:destination])\n",
    "        return d if d<Sum-d else Sum-d\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start < destination:\n",
    "            a = sum(distance[:start])\n",
    "            b = sum(distance[destination:])\n",
    "        else:\n",
    "            a = sum(distance[:destination])\n",
    "            b = sum(distance[start:])\n",
    "        ans = sum(distance)\n",
    "        return ans - (a + b) if 2 * (a + b) > ans else a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        x = min(start, destination)\n",
    "        y = max(start, destination)\n",
    "        p1 = 0\n",
    "        p2 = 0\n",
    "        for i in range (len(distance)):\n",
    "            if i >= x and i < y:\n",
    "                p1 += distance[i]\n",
    "            p2 += distance[i]\n",
    "        return min(p1, p2 - p1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            return self.distanceBetweenBusStops(distance, destination, start)\n",
    "        n = len(distance)\n",
    "        ans = 0\n",
    "        # 顺时针\n",
    "        for i in range(start, destination):\n",
    "            ans += distance[i]\n",
    "        # 逆时针\n",
    "        ans = min(ans, sum(distance)-ans)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        t = reduce(add,distance)\n",
    "        if start <= destination:\n",
    "            num = sum(distance[start:destination])\n",
    "        else :\n",
    "            num = sum(distance[destination:start])\n",
    "        return num if num <= t-num else t-num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        #判断初始条件\n",
    "        disc = 0\n",
    "        if start == destination:\n",
    "            return 0\n",
    "        elif start < destination:#正向\n",
    "            disc = sum(distance[start : destination])\n",
    "        else:#逆向\n",
    "            disc = sum(distance[destination : start])\n",
    "        return min(disc, sum(distance) - disc)#正向反向之和为list总和sum(distance), 取较小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        s = sum(distance)\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        d = 0\n",
    "        for x in range(start, destination):\n",
    "            d += distance[x]\n",
    "        return min(d, s - d)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        x = min(start, destination)\n",
    "        y = max(start, destination)\n",
    "        p1 = 0\n",
    "        p2 = 0\n",
    "        for i in range (len(distance)):\n",
    "            if i >= x and i < y:\n",
    "                p1 += distance[i]\n",
    "            p2 += distance[i]\n",
    "        return min(p1, p2 - p1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        sum_i = 0\n",
    "        if start > destination:\n",
    "            start,destination = destination,start\n",
    "        for i in range(start,destination):\n",
    "            sum_i = sum_i + distance[i]\n",
    "        sum_ls = sum(distance)\n",
    "        return min(sum_i,sum_ls - sum_i)'''\n",
    "'''\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))\n",
    "'''\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        total, s, start, destination = 0, 0, min(start, destination), max(start, destination)\n",
    "        for i, d in enumerate(distance):\n",
    "            total += d\n",
    "            if start <= i < destination:\n",
    "                s += d\n",
    "        return min(s, total - s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        \n",
    "        if start == destination:\n",
    "            return 0\n",
    "        if start > destination:\n",
    "            start,destination = destination, start\n",
    "        total = sum(distance)\n",
    "        count = sum(distance[start:destination])\n",
    "        remain = total-count\n",
    "        return min(count,remain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start>destination:\n",
    "            start,destination=destination,start\n",
    "        a=sum(distance[start:destination])\n",
    "        b=sum(distance[:start])+sum(distance[destination:])\n",
    "        return min(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start == destination:\n",
    "            return 0\n",
    "        sum = 0\n",
    "        temp1 = 0\n",
    "        for i in range(len(distance)):\n",
    "            sum += distance[i]\n",
    "            if i >= min(start, destination) and i < max(start, destination):\n",
    "                temp1 += distance[i]\n",
    "        return min(temp1, sum - temp1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        mx=sum(distance)\n",
    "        if destination==start:\n",
    "            return 0\n",
    "        cnt=0\n",
    "        n=len(distance)\n",
    "        if destination>start:\n",
    "            for i in range(start,destination):\n",
    "                cnt+=distance[i]\n",
    "        else:\n",
    "            for i in range(start-1,destination-1,-1):\n",
    "                cnt+=distance[i]\n",
    "\n",
    "        return min(cnt,mx-cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        first = 0\n",
    "        second = 0\n",
    "        x = min(start, destination)\n",
    "        y = max(start, destination)\n",
    "        n = len(distance)\n",
    "        for i in range(n):\n",
    "            if i < x:\n",
    "                first += distance[i]\n",
    "            elif x <= i < y:\n",
    "                second += distance[i]\n",
    "            else:\n",
    "                first += distance[i]\n",
    "        print(first, second)\n",
    "        return min(first, second)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        total, s, start, destination = 0, 0, min(start, destination), max(start, destination)\n",
    "        for i, d in enumerate(distance):\n",
    "            total += d\n",
    "            if start <= i <destination:\n",
    "                s += d\n",
    "        return min(s, total -s )\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start>destination:\n",
    "            start,destination = destination,start\n",
    "        return min(sum(distance[start:destination]),sum(distance[:start])+sum(distance[destination:]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "            min_value = min(start, destination)\n",
    "            max_value = max(start, destination)\n",
    "\n",
    "            return min(sum(distance[min_value: max_value]), sum(distance[max_value:] + distance[:min_value]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:    \n",
    "        if destination < start: # 交换出发点和目的地距离相等\n",
    "            start, destination = destination, start            \n",
    "        d = sum(distance[start : destination]) # 出发点到目的地距离\n",
    "        return min(d, sum(distance) - d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\r\n",
    "        n = len(distance)\r\n",
    "        a = min(start, destination)\r\n",
    "        b = max(start, destination)\r\n",
    "        \r\n",
    "        cur = 0\r\n",
    "        for i in range(a, b):\r\n",
    "            cur += distance[i]\r\n",
    "        res = cur\r\n",
    "        \r\n",
    "        cur = 0\r\n",
    "        for i in range(b, a + n):\r\n",
    "            cur += distance[i % n]\r\n",
    "        res = min(res, cur)\r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        return min(sum(distance[start:destination]),\\\n",
    "                    sum(distance[destination:])+sum(distance[:start]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        start, destination = min(start, destination), max(start, destination)\n",
    "        x = sum(distance[start:destination])\n",
    "        return min(x, sum(distance[destination:])+sum(distance[:start]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        one,two=0,0\n",
    "        if start<destination:\n",
    "            for i in range(start,destination):\n",
    "                one=one+distance[i]\n",
    "            two=sum(distance)-one\n",
    "        else:\n",
    "            for i in range(destination,start):\n",
    "                one=one+distance[i]\n",
    "            two=sum(distance)-one\n",
    "        return min(one,two)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        s=sum(distance)\n",
    "        if start>destination:\n",
    "            start,destination=destination,start\n",
    "        r=0\n",
    "        while start<destination:\n",
    "            r+=distance[start]\n",
    "            start+=1\n",
    "        return min(r,s-r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        ans = 0\n",
    "        if start > destination:\n",
    "            start, destination = destination, start \n",
    "        return min(sum(distance[start:destination]), sum(distance) - sum(distance[start:destination]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        d = sum(distance[min(start, destination):max(start, destination)])\n",
    "        return min(d, sum(distance) - d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        # x-y  d[x]-d[y-1]\n",
    "        if start>destination:\n",
    "            start,destination = destination,start\n",
    "        l = sum(distance[start:destination])\n",
    "        r = sum(distance)-l\n",
    "        return (min(l,r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        total = sum(distance)\n",
    "        go = sum(distance[min(destination,start):max(destination,start)])\n",
    "        return min(go,total-go)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        a=b=0\n",
    "        if start<destination:\n",
    "            for i in range(start,destination,1):\n",
    "                a=a+distance[i]\n",
    "        else:\n",
    "            for i in range(destination,start,1):\n",
    "                a=a+distance[i]\n",
    "        for i in range(len(distance)):\n",
    "            b=b+distance[i]\n",
    "        return min(a,b-a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "            min_value = min(start, destination)\n",
    "            max_value = max(start, destination)\n",
    "\n",
    "            return min(sum(distance[min_value: max_value]), sum(distance[max_value:] + distance[:min_value]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "            min_value = min(start, destination)\n",
    "            max_value = max(start, destination)\n",
    "\n",
    "            return min(sum(distance[min_value: max_value]), sum(distance[max_value:] + distance[:min_value]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        n = len(distance)\n",
    "        a = sum(distance)\n",
    "        b = 0\n",
    "        if start <= destination:\n",
    "            for x in distance[start:destination]:\n",
    "                b += x\n",
    "        else:\n",
    "            for x in distance[destination:start]:\n",
    "                b += x\n",
    "        return min(a - b, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        n = len(distance)\n",
    "        pre = [0] * (n+1)\n",
    "        for i in range(1, n+1):\n",
    "            pre[i] = pre[i-1] + distance[i-1] \n",
    "        \n",
    "        tot =pre[-1]\n",
    "        s,d = min(destination, start), max(destination, start) \n",
    "        return min(pre[d] - pre[s], tot-pre[d] + pre[s])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:\n",
    "        dis = back_dis = 0\n",
    "        if start > destination:\n",
    "            start, destination = destination, start\n",
    "        '''\n",
    "        \n",
    "        for i in range(start, destination):\n",
    "            dis += distance[i]\n",
    "        \n",
    "        for i in range(destination, len(distance)):\n",
    "            back_dis += distance[i]\n",
    "        return min(dis, back_dis)\n",
    "        '''\n",
    "        left = min(start, destination)\n",
    "        right = max(start, destination)\n",
    "        dis = sum(distance[left : right])\n",
    "        back_dis = sum(distance[:left]) + sum(distance[right:])\n",
    "        return min(dis, back_dis)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
